<template>
  <div class="box">
    <h2>Son1 子组件</h2>
    从vuex中获取的值: <label>{{$store.state.count}}</label>
    <br>
    <button @click="handleAdd(1)">值 + 1</button>
    <button @click="handleAdd(5)">值 + 5</button>
    <button @click="handleAdd(10)">值 + 10</button>
    <button @click="changeFn">改标题</button>
    <button @click="handleChange">1s后改成666</button>
    <br>
    <div>{{$store.state.list}}</div>
    <div>{{$store.getters.filterList}}</div>
    <hr>
    <!-- 测试访问模块里面的state -->
    <div>{{$store.state.user.userInfo.name }}</div>
    <button @click="updateUser">更新个人信息</button>
    <button @click="updateUser2">一秒后更新信息</button>
    <div>{{$store.state.setting.theme}}</div>
    <button @click="updateTheme">更新主题色</button>
    <button @click="updateTheme2">一秒后更新主题色</button>
    <!-- 测试访问模块里面的getters -->
    <div>{{ $store.getters['user/UpperCaseName']}}</div>
    </div>
</template>

<script>
export default {
  name: 'Son1Com',
  methods: {
    updateUser () {
      this.$store.commit('user/setUser', {
        name: '小王',
        age: 20
      })
    },
    updateTheme () {
      this.$store.commit('setting/setTheme', 'dark')
    },
    updateUser2 () {
      this.$store.dispatch('user/setUserSecond', {
        name: '小王',
        age: 20
      })
    },
    updateTheme2 () {
      this.$store.dispatch('setting/setThemeSecond', 'blue')
    },
    handleAdd (n) {
      // vuex的一种错误写法，不遵守单向数据流
      // this.$store.state.count++
      // console.log(this.$store.state.count)

      // 通过mutation进行修改数据
      // this.$store.commit('addCount')
      console.log(n)
      this.$store.commit('addCount', {
        count: n,
        msg: '111'
      })
    },
    // addFive () {
    //   this.$store.commit('addFive')
    // },
    changeFn () {
      this.$store.commit('changeTitle', '我是架构师')
    },
    handleChange () {
      this.$store.dispatch('changeCountAction', 666)
    }
  }
}
</script>

<style lang="css" scoped>
.box{
  border: 3px solid #ccc;
  width: 400px;
  padding: 10px;
  margin: 20px;
}
h2 {
  margin-top: 10px;
}
</style>
